package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.ProjectMapper;
import com.youlai.boot.system.service.ProjectService;
import com.youlai.boot.system.model.entity.Project;
import com.youlai.boot.system.model.form.ProjectForm;
import com.youlai.boot.system.model.query.ProjectQuery;
import com.youlai.boot.system.model.vo.ProjectVO;
import com.youlai.boot.system.converter.ProjectConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 项目实践服务实现类
 *
 * @author youlaitech
 * @since 2025-03-15 10:58
 */
@Service
@RequiredArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    private final ProjectConverter projectConverter;

    /**
     * 获取项目实践分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ProjectVO>} 项目实践分页列表
     */
    @Override
    public IPage<ProjectVO> getProjectPage(ProjectQuery queryParams) {
        Page<Project> projectPage = this.baseMapper.getProjectPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        List<ProjectVO> projects = projectPage.getRecords().stream().map(projectConverter::toProjectVo).collect(Collectors.toCollection(ArrayList::new));
        Page<ProjectVO> projectVoPage = new Page<>(projectPage.getCurrent(),projectPage.getSize());
        projectVoPage.setRecords(projects);
        return projectVoPage;
    }

    /**
     * 获取项目实践表单数据
     *
     * @param id 项目实践ID
     * @return
     */
    @Override
    public ProjectForm getProjectFormData(Long id) {
        Project entity = this.getById(id);
        return projectConverter.toForm(entity);
    }

    /**
     * 新增项目实践
     *
     * @param formData 项目实践表单对象
     * @return
     */
    @Override
    public boolean saveProject(ProjectForm formData) {
        Project entity = projectConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新项目实践
     *
     * @param id   项目实践ID
     * @param formData 项目实践表单对象
     * @return
     */
    @Override
    public boolean updateProject(Long id,ProjectForm formData) {
        Project entity = projectConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除项目实践
     *
     * @param ids 项目实践ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteProjects(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的项目实践数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
